/**
 * Copyright (C), 2020-2020, 软件公司
 * FileName: ConnectionUtils.java
 * Author:   cakin
 * Date:     2020/4/25
 * Description: 获取rabbitmq连接
 */
package com.rabbitmq.util;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * @className: ConnectionUtils
 * @description: 获取rabbitmq连接
 * @date: 2020/4/25
 * @author: cakin
 */
public class ConnectionUtils {
    /**
     * 主机
     */
    private static final String HOST = "127.0.0.1";

    /**
     * RabbitMQ（AMQP） 服务端默认端口号为 5672
     */
    private static final int PORT = 5672;

    /**
     * 虚拟主机
     */
    private static final String VIRTUALHOST = "/";

    /**
     * 用户名
     */
    private static final String USERNAME = "root";

    /**
     * 密码
     */
    private static final String PASSWORD = "123456";

    /**
     * 功能描述：获取RabbitMQ Connection连接
     *
     * @return Connection
     * @throws IOException      IOException
     * @throws TimeoutException TimeoutException
     * @author cakin
     * @date 2020/4/25
     * @description:
     */
    public static Connection getConnection() throws IOException, TimeoutException {
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setHost(HOST);
        connectionFactory.setPort(PORT);

        /**
         * 如果配置有用户名密码以及vhost，则配置即可。
         */
        connectionFactory.setUsername(USERNAME);
        connectionFactory.setPassword(PASSWORD);
        connectionFactory.setVirtualHost(VIRTUALHOST);

        return connectionFactory.newConnection();
    }

    /**
     * 功能描述：获取RabbitMQ Connection连接
     *
     * @param host        host
     * @param port        port
     * @param virtualHost virtualHost
     * @param username    username
     * @param password    password
     * @return Connection
     * @throws IOException      IOException
     * @throws TimeoutException TimeoutException
     * @author cakin
     * @date 2020/4/25
     */
    public static Connection getConnection( String host, Integer port, String virtualHost,
                                            String username, String password ) throws IOException, TimeoutException {
        // 连接工厂
        ConnectionFactory factory = new ConnectionFactory();
        // ip
        factory.setHost(host);
        // 端口号
        factory.setPort(port);
        // 虚拟主机
        factory.setVirtualHost(virtualHost);
        factory.setUsername(username);
        factory.setPassword(password);
        return factory.newConnection();
    }

    /**
     * 功能描述：关闭RabbitMQ连接
     *
     * @param channel    channel
     * @param connection connection
     * @throws IOException      IOException
     * @throws TimeoutException TimeoutException
     * @author cakin
     * @date 2020/4/25
     */
    public static void close( Channel channel, Connection connection ) throws IOException, TimeoutException {
        if (channel != null) {
            channel.close();
        }

        if (connection != null) {
            connection.close();
        }
    }
}
